package com.student.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.student.demo.config.CommonPage;
import com.student.demo.config.Result;
import com.student.demo.dto.StudentGradeDTO;
import com.student.demo.dto.StudentInfoDTO;
import com.student.demo.entity.StudentGrade;
import com.student.demo.entity.StudentInfo;
import com.student.demo.mapper.StudentGradeMapper;
import com.student.demo.service.IStudentGradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentGradeService extends ServiceImpl<StudentGradeMapper, StudentGrade> implements IStudentGradeService {

    @Transactional
    @Override
    public Result<?> insert(StudentGrade studentGrade) {
        save(studentGrade);
        return Result.success();
    }

    @Transactional
    @Override
    public Result<?> delInfo(Long id) {
        try {
            StudentGrade byId = getById(id);
            LambdaUpdateChainWrapper<StudentGrade> updateWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
            updateWrapper.eq(StudentGrade::getId, id)
                    .set(StudentGrade::getIsDelete, 1)
                    .update();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @Transactional
    @Override
    public Result<?> updateInfo(StudentGrade studentGrade) {
        try {
            StudentGrade byId = getById(studentGrade.getId());
            LambdaUpdateChainWrapper<StudentGrade> updateWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
            updateWrapper.eq(StudentGrade::getId, studentGrade.getId()).update(studentGrade);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @Override
    public Result<?> listPage(StudentGradeDTO studentGradeDTO) {
        // 设置页数和行数
        PageHelper.startPage(studentGradeDTO.getPageNum(), studentGradeDTO.getPageSize());
        QueryWrapper<StudentGrade> queryWrapper = new QueryWrapper<>();
        // 查询姓名
        String stuName = studentGradeDTO.getStuName();
        if (StringUtils.isNotBlank(stuName)){
            queryWrapper.lambda().eq(StudentGrade::getStuName, stuName);
        }
        // 查询学号
        String stuId = studentGradeDTO.getStuId();
        if (StringUtils.isNotBlank(stuId)){
            queryWrapper.lambda().eq(StudentGrade::getStuId, stuId);
        }
        // 逻辑删除查询
        Integer isDelete = studentGradeDTO.getIsDelete();
        if (isDelete==0){
            queryWrapper.lambda().eq(StudentGrade::getIsDelete, isDelete);
        }
        // 分页
        List<StudentGrade> list = list(queryWrapper);
        CommonPage<StudentGrade> pageInfo = new CommonPage<>().restPage(list);
        return Result.success(pageInfo);
    }
}
